﻿using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Drawing;
using System.Linq;
using System.Web;

namespace Beryl.OrderControllers.DBUpdate
{
    public class totalUpdate
    {
        private static totalUpdate instance = null;
        private static readonly object uniqueLock = new object();
        public static totalUpdate Instance
        {
            get
            {
                lock (uniqueLock)
                {
                    if (instance == null)
                    {
                        instance = new totalUpdate();
                    }
                }
                return instance;
            }
        }
        public totalUpdate() { }

        /**
 * @description 删除需要更新的销售订单
 * @param {*}  
 */
        public void deleteSoNo(Object obj)
        {
            var cSONoArr = (Dictionary<string, Dictionary<string, string>>)obj;
            if(cSONoArr.Count == 0)
            {
                return;
            }
            List<string> sqlArr = new List<string>();
            try
            {
                foreach(var cPOrderNo in cSONoArr.Keys)
                {
                    string sqlString = string.Format(@"DELETE FROM APS_ValidBOM WHERE cOrderNo = '{0}';", cPOrderNo);
                    sqlArr.Add(sqlString);
                }
                bool ok = CommonMethod.Instance.insertSqlList(sqlArr);
                if (!ok)
                {
                    throw new Exception("删除需要更新的销售订单失败");
                }
            }
            catch(Exception e)
            {
                throw e;
            }
        }

        /**
 * @description 需要更新的销售订单
 * @param {*}  
 */
        public void updateSoNo(Object obj)
        {
            var cSONoArr_new = (Dictionary<string, Dictionary<string, string>>)obj;
            List<string> sqlArr = new List<string>();
            try
            {
                foreach(var cPOrderNo in cSONoArr_new.Keys)
                {
                    string sqlString = string.Format(@"UPDATE APS_PlanOrder SET cStatusNo = '0032', cStaUpdateTime = convert(varchar(10),getdate(),120) WHERE cPOrderNo = '{0}';", cPOrderNo);
                    sqlArr.Add(sqlString);
                }
                bool ok = CommonMethod.Instance.insertSqlList(sqlArr);
                if (!ok)
                {
                    throw new Exception("更新的销售订单失败");
                }
            }
            catch(Exception e)
            {
                throw e;
            }
        }

        /**
 * @description 插入物料总需求
 * @param {*}  cMatTotalQty
 */
        public void insertMatTotalQty(Object obj)
        {
            var cMatTotalQty = (Dictionary<string, Dictionary<string, List<Dictionary<string, string>>>>)obj;
            List<string> sqlArr = new List<string>();
            try
            {
                string sqlString = "INSERT INTO APS_ValidBOM (cOrderNo,cOrderType,cMatNo,cOperationNo,cOperationName,cOpSeq,cInOutType,cMatandResNo," +
                    "cMatandResName,cMatandResType,dPlanQty,dwidth,dlength,cSpecifution,cUnit,dReqQty,cCapacityLow,cCapacityup,cCapacity,cBomid,cCateNo," +
                    "dVoltage,Unit,cSortSeq) SELECT * FROM ( VALUES ";
                string sqlString1 = "";
                int length1 = 0;
                int length2 = cMatTotalQty.Count;
                foreach(var cPOrderNo in cMatTotalQty.Keys)
                {
                    length1 = length1 + 1;
                    string cOrderNo = cPOrderNo;
                    int length3 = 0;
                    int length4 = cMatTotalQty[cOrderNo].Count;
                    if(length4 <= 0)
                    {
                        if (length1 == length2)
                        {
                            sqlString1 = sqlString1.Substring(0, sqlString1.Length - 1);
                        }
                        else
                        {
                            continue;
                        }
                    }
                    foreach (var cMatNo in cMatTotalQty[cPOrderNo].Keys)
                    {
                        length3 = length3 + 1;
                        int length5 = 0;
                        int length6 = cMatTotalQty[cPOrderNo][cMatNo].Count;
                        if (length6 == 0)
                        {
                            sqlString1 = sqlString1.Substring(0, sqlString1.Length - 1);
                        }
                        else
                        {
                        }
                        for(int i = 0; i < cMatTotalQty[cPOrderNo][cMatNo].Count; i++)
                        {
                            length5++;
                            string cOrderType = cMatTotalQty[cPOrderNo][cMatNo][i]["cOrderType"];
                            string ResNo = cMatTotalQty[cPOrderNo][cMatNo][i]["ResNo"];
                            string cOperationNo = cMatTotalQty[cPOrderNo][cMatNo][i]["cOperationNo"];
                            string cOperationName = cMatTotalQty[cPOrderNo][cMatNo][i]["cOperationName"];
                            string cOpSeq = cMatTotalQty[cPOrderNo][cMatNo][i]["cOpSeq"];
                            string cInOutType = cMatTotalQty[cPOrderNo][cMatNo][i]["cInOutType"];
                            string tmp_proNo = cMatTotalQty[cPOrderNo][cMatNo][i]["tmp_proNo"];
                            string cMatandResName = cMatTotalQty[cPOrderNo][cMatNo][i]["cMatandResName"];
                            string cMatandResType = cMatTotalQty[cPOrderNo][cMatNo][i]["cMatandResType"];
                            string dPlanQty = cMatTotalQty[cPOrderNo][cMatNo][i]["dPlanQty"] == "" ? "NULL" : cMatTotalQty[cPOrderNo][cMatNo][i]["dPlanQty"];
                            string dwidth = cMatTotalQty[cPOrderNo][cMatNo][i]["dwidth"] == "" ? "NULL" : cMatTotalQty[cPOrderNo][cMatNo][i]["dwidth"];
                            string dlength = cMatTotalQty[cPOrderNo][cMatNo][i]["dlength"] == "" ? "NULL" : cMatTotalQty[cPOrderNo][cMatNo][i]["dlength"];
                            string cSpecifucation = cMatTotalQty[cPOrderNo][cMatNo][i]["cSpecifucation"];
                            string cUnit = cMatTotalQty[cPOrderNo][cMatNo][i]["cUnit"];
                            string TotalQty = cMatTotalQty[cPOrderNo][cMatNo][i]["TotalQty"];
                            string cCapacityLow = cMatTotalQty[cPOrderNo][cMatNo][i]["cCapacityLow"] == "" ? "NULL" : cMatTotalQty[cPOrderNo][cMatNo][i]["cCapacityLow"];
                            string cCapacityup = cMatTotalQty[cPOrderNo][cMatNo][i]["cCapacityup"] == "" ? "NULL" : cMatTotalQty[cPOrderNo][cMatNo][i]["cCapacityup"];
                            string cCapacity = cMatTotalQty[cPOrderNo][cMatNo][i]["cCapacity"] == "" ? "NULL" : cMatTotalQty[cPOrderNo][cMatNo][i]["cCapacity"];
                            string cBomid = cMatTotalQty[cPOrderNo][cMatNo][i]["cBomid"];
                            string cCateNo = cMatTotalQty[cPOrderNo][cMatNo][i]["cCateNo"];
                            string dVoltage = cMatTotalQty[cPOrderNo][cMatNo][i]["dVoltage"] == "" ? "NULL" : cMatTotalQty[cPOrderNo][cMatNo][i]["dVoltage"];
                            string Unit = cMatTotalQty[cPOrderNo][cMatNo][i]["Unit"] == "" ? "NULL" : cMatTotalQty[cPOrderNo][cMatNo][i]["Unit"];
                            string cSortSeq = cMatTotalQty[cPOrderNo][cMatNo][i]["SortSeq"];

                            if((length1 == length2) && (length3 == length4) && (length5 == length6))
                            {
                                sqlString1 += string.Format(@"('{0}','{1}','{2}','{3}','{4}','{5}',
                        '{6}','{7}','{8}','{9}',{10},{11},{12},
                        '{13}','{14}','{15}',{16},{17},{18},'{19}',
                        '{20}',{21},'{22}','{23}')", cOrderNo, cOrderType, ResNo, cOperationNo, cOperationName, cOpSeq, cInOutType, tmp_proNo, cMatandResName, cMatandResType, dPlanQty,
                        dwidth, dlength, cSpecifucation, cUnit, TotalQty, cCapacityLow, cCapacityup, cCapacity, cBomid, cCateNo, dVoltage, Unit, cSortSeq);
                            }else
                            {
                                sqlString1 += string.Format(@"('{0}','{1}','{2}','{3}','{4}','{5}',
                        '{6}','{7}','{8}','{9}',{10},{11},{12},
                        '{13}','{14}','{15}',{16},{17},{18},'{19}',
                        '{20}',{21},'{22}','{23}'),", cOrderNo, cOrderType, ResNo, cOperationNo, cOperationName, cOpSeq, cInOutType, tmp_proNo, cMatandResName, cMatandResType, dPlanQty,
                        dwidth, dlength, cSpecifucation, cUnit, TotalQty, cCapacityLow, cCapacityup, cCapacity, cBomid, cCateNo, dVoltage, Unit, cSortSeq);
                            }
                        }
                    }
                }
                // 数据库更新
                if (sqlString1 != "")
                {
                    sqlString += sqlString1 + ") A(Col1, Col2, Col3, Col4, Col5, Col6, Col7, Col8, Col9, Col10, Col11, Col12, Col13, Col14, Col15, Col16, Col17, Col18, Col19, Col20, Col21, Col22, Col23, Col24);";
                    sqlArr.Add(sqlString);
                }
                bool ok = CommonMethod.Instance.insertSqlList(sqlArr);
                if (!ok)
                {
                    throw new Exception("插入物料总需求失败");
                }
            }
            catch(Exception e)
            {
                throw e;
            }
        }
    }
}